/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.classes.Staff;
import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.awt.Graphics;
import java.awt.Image;
import java.text.SimpleDateFormat;
import javax.swing.ImageIcon;

import java.util.HashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Jason
 */
public class OptionChangePasswordPanel extends javax.swing.JPanel {

    Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    ButtonGroup bgSchedule = new ButtonGroup();
    String[][] myStringArray = new String [0][1];
    private int totalRadioButtonGenerated = 0;
    JRadioButton jRadioButton[] = new JRadioButton[0];
    Staff s = new Staff();
    /**
     * Creates new form RoutePanel
     */
    public OptionChangePasswordPanel() {
        initComponents();
        jtfID.setEnabled(false);
        jpCurrentPassword.setText(null);
        jpNewPassword.setText(null);
        jpConfirmPassword.setText(null);
        
    }
    
    


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1 = new java.awt.Label();
        jpStaffArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };
        jpStaff = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jpCurrentPassword = new javax.swing.JPasswordField();
        jpConfirmPassword = new javax.swing.JPasswordField();
        jpNewPassword = new javax.swing.JPasswordField();

        label1.setText("label1");

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        javax.swing.GroupLayout jpStaffArrowLayout = new javax.swing.GroupLayout(jpStaffArrow);
        jpStaffArrow.setLayout(jpStaffArrowLayout);
        jpStaffArrowLayout.setHorizontalGroup(
            jpStaffArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpStaffArrowLayout.setVerticalGroup(
            jpStaffArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpStaffArrow);
        jpStaffArrow.setBounds(0, 0, 150, 20);

        jpStaff.setBackground(new java.awt.Color(220, 220, 220));
        jpStaff.setAutoscrolls(true);
        jpStaff.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Change Password"));

        jLabel1.setText("ID");

        jLabel2.setText("Current Password");

        jLabel3.setText("New Password");

        jLabel4.setText("Confirm Password");

        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfID, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(jpCurrentPassword)
                            .addComponent(jpConfirmPassword)
                            .addComponent(jpNewPassword))))
                .addGap(36, 36, 36))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jpCurrentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jpNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jpConfirmPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(161, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(491, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jpStaff.add(jPanel1);
        jPanel1.setBounds(10, 10, 850, 420);

        add(jpStaff);
        jpStaff.setBounds(0, 20, 870, 440);
        jpStaff.getAccessibleContext().setAccessibleName("ticketForm");
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jtfID.setText(""+s.getUid().toString());
        
        String currentPassword = new String(jpCurrentPassword.getPassword());
        String confirmPassword = new String(jpConfirmPassword.getPassword());
        String newPassword = new String(jpNewPassword.getPassword());
        
        
        Map<String, Object> data = new HashMap<>();
        data.put("uid",jtfID.getText());
        data.put("cPassword",currentPassword);
        data.put("nPassword",newPassword);
        data.put("cmPassword",confirmPassword);

        if(jtfID.getText().length()!=0){
            if(jpCurrentPassword.getPassword().length > 0){
                if(jpNewPassword.getPassword().length > 5 && jpNewPassword.getPassword().length < 20){
                    if(jpConfirmPassword.getPassword().length > 5 && jpConfirmPassword.getPassword().length < 20){
                        if(newPassword.equals(confirmPassword)){
                            try{
                            String a = RestServer.httpPost("changePassword",data);
                            JSONObject json = new JSONObject(a);
                            JSONObject content = json.getJSONObject("content");
                            if(json.getInt("status")==200){
                                if(content.getBoolean("exist")){
                                    if(!content.getBoolean("err")){
                                        Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                        jpCurrentPassword.setText(null);
                                        jpNewPassword.setText(null);
                                        jpConfirmPassword.setText(null);
                                    }else{
                                        Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                    }
                                }else{
                                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                }
                            }else{
                                Toast.makeText(getParent(), "404! Not Found!", Toast.LENGTH_LONG).display();
                            }
                            }catch(JSONException ex){
                                Toast.makeText(getParent(), "Connection error. Please try again later", Toast.LENGTH_LONG).display();
                            }
                        }else{
                            Toast.makeText(getParent(), "New and confirm password not match", Toast.LENGTH_LONG).display();
                        }
                    }else if(jpConfirmPassword.getPassword().length > 20){
                        Toast.makeText(getParent(), "Confirm password must not longer than 20 characters", Toast.LENGTH_LONG).display();
                    }else{
                        Toast.makeText(getParent(), "Confirm password must between 6 to 20 characters", Toast.LENGTH_LONG).display();
                    }
                }else if(jpNewPassword.getPassword().length > 20){
                    Toast.makeText(getParent(), "New password must not longer than 20 characters", Toast.LENGTH_LONG).display();
                }else{
                    Toast.makeText(getParent(), "New password must between 6 to 20 characters", Toast.LENGTH_LONG).display();
                }

            }else{
                Toast.makeText(getParent(), "Current password field must not be empty", Toast.LENGTH_LONG).display();
            }
        }else{
            Toast.makeText(getParent(), "System Error, Please login again", Toast.LENGTH_LONG).display();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    
    
    


    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jpConfirmPassword;
    private javax.swing.JPasswordField jpCurrentPassword;
    private javax.swing.JPasswordField jpNewPassword;
    private javax.swing.JPanel jpStaff;
    private javax.swing.JPanel jpStaffArrow;
    private javax.swing.JTextField jtfID;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables
}
